import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

filename = r'F:\rice.png' 
sourceimg = cv.imread(filename)
grayimg = cv.cvtColor(sourceimg,cv.COLOR_BGR2GRAY)

img = cv.imread(filename)
color = ('b', 'g', 'r')
for i, col in enumerate(color):
    histr = cv.calcHist([img], [i], None, [256], [0, 256])
    plt.plot(histr, color=col)
    plt.xlim([0, 256])
plt.show()

gray,otsu = cv.threshold(grayimg,0,255,cv.THRESH_OTSU)
print(gray)
cv.imshow('otsu',otsu)
cv.waitKey(0)



